#include"array.h"
#include<algorithm>

Array::Array (size_t s){
	p = new int [s];
	size = s;
	capacity = s;
}

Array::Array (const Array & a){
	p = new int [a.size];
	size = a.size;
	capacity = a.capacity;
	int *q = a.p;
	for (int i = 0; i < a.size; i ++){
		p[i] = q[i];
	}
}

Array::~Array (){
	delete []p;
}

Array & Array::operator= (const Array & a){
	Array tmp(a);
	swap(tmp);
	return *this;
}

int Array::get (size_t index) const{
	int *q = p;
	if (index < size) {
		q += index;
		return *q;
	}
	return 0;
}
	
void Array::set (size_t index, int value){
	int *q = p;
	if (index < size){
		q += index;
		*q = value;
	}
}

void Array::push_back (int value){
	if (size == capacity){
		int *q = new int [2*size];
		capacity = 2*size;
		for (int i = 0; i < size; i++){
			q[i] = p[i];
		}
		delete []p;
		p = q;
	}
	p[size] = value;
	size++;
}

size_t Array::get_size () const{
	return size;	
}

size_t Array::get_capacity() const{
	return capacity;
}

void Array::swap (Array & a){
   	std::swap(size,a.size);
	std::swap(p, a.p);
	std::swap(capacity, a.capacity);
}   








